package br.uefs.dexa.rfframework.gui.utils.paging;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextAttribute;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class PagingLabel extends JLabel {
	
	
	private static final long serialVersionUID = 8573950108691257715L;
	
	protected static final Font ORIGINAL_FONT = new Font("Arial", Font.PLAIN, 24);
	
	protected static final Color ORIGINAL_COLOR = Color.BLACK;
	
	protected static final Color SELECTED_COLOR = Color.RED;
	
	protected static final Color MOUSE_HOVER_COLOR = Color.BLUE;
	
	protected static final int BORDER_THICKNESS = 1;

	private int page;
	
	private boolean currentPage = false;
	
	private PagingOutputObjectSetContainerPanel pagingPanel;
	
	
	protected PagingLabel(int page, PagingOutputObjectSetContainerPanel pagingPanel)
	{
		super(page + "");
		this.pagingPanel = pagingPanel;
		this.page = page;
		
		setBorder(BorderFactory.createEmptyBorder(BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS));
		setFont (ORIGINAL_FONT);
		addMouseListener();
	}
	
	protected void reset()
	{
		if (!SwingUtilities.isEventDispatchThread()) {
			
			try {
				EventQueue.invokeAndWait(new Runnable() {
					public void run() 
					{
						currentPage = false;
						setFont (ORIGINAL_FONT);
						setForeground(ORIGINAL_COLOR);
						setBorder(BorderFactory.createEmptyBorder(BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS));	
						revalidate();
						repaint();
					}
				});
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		
		} else {
			currentPage = false;
			setFont (ORIGINAL_FONT);
			setForeground(ORIGINAL_COLOR);
			setBorder(BorderFactory.createEmptyBorder(BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS));
			revalidate();
			repaint();
		}
	}
	
	protected boolean isCurrentPage()
	{
		return currentPage;
	}
	
	protected void markAsCurrentPage()
	{
		currentPage = true;
		setBorder(BorderFactory.createEmptyBorder(BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS));
		setForeground(SELECTED_COLOR);
		
		setUnderlined();
		
	}
	
	private void setUnderlined()
	{
		Font font = getFont();
		Map attributes = font.getAttributes();
		attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
		setFont(font.deriveFont(attributes));
	}
	
	
	protected void removeMouseListeners()
	{
		MouseListener[] ml = getMouseListeners();
		
		for (int i = 0; i < ml.length; i++) {
			
			MouseListener mouseListener = ml[i];
			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					removeMouseListener(mouseListener);
				}
			});			
		}
	}
	
	protected void addMouseListener()
	{
		addMouseListener( new MouseAdapter() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				
				if(!isCurrentPage()) {
					reset();
				} else {
					markAsCurrentPage();					
				}
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				setBorder(BorderFactory.createLineBorder(MOUSE_HOVER_COLOR, BORDER_THICKNESS));
				setForeground(MOUSE_HOVER_COLOR);	
			}
			
			@Override
			public void mouseClicked(MouseEvent e) 
			{	
				pagingPanel.setLimits(page);
				
				Thread t = new Thread( new Runnable() {
					@Override
					public void run() {
						pagingPanel.showPage();
						
					}
				});
				t.start();
				
				pagingPanel.resetLabels();
				markAsCurrentPage();
			}
		});
	}

}
